﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="EcommJewellery.SearchResultsPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:EcommJewellery"
    xmlns:common="using:EcommJewellery.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:ExtensionType="Search">

    <Page.Resources>
        <CollectionViewSource x:Name="resultsViewSource" Source="{Binding Results}"/>
        <CollectionViewSource x:Name="filtersViewSource" Source="{Binding Filters}"/>
        <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <!-- TODO: Update the following string to be the name of your app -->
        <DataTemplate x:Key="NecklacesBigItemTemplate">
            <Grid HorizontalAlignment="Left" Width="165" Height="165" Background="White">
            <Border>
                <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
            </Border>
            <StackPanel VerticalAlignment="Bottom" Margin="10,0,10,5">
            	<TextBlock Text="{Binding Title}" Foreground="#B2454545" Margin="0" FontSize="13.333" FontWeight="Normal" FontFamily="Segoe UI"/>
            	<StackPanel Margin="0" Orientation="Horizontal">
            		<Image Width="9" Source="ms-appx:///Assets/HubPageImages/RsIcon.png" VerticalAlignment="Center"/>
            		<TextBlock Text="{Binding Subtitle}" Foreground="#B2454545"  TextWrapping="NoWrap" FontSize="16" Margin="2,0,0,0" FontWeight="SemiLight" VerticalAlignment="Center" FontFamily="Segoe UI"/>
            	</StackPanel>
            </StackPanel>
        </Grid>
        </DataTemplate>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.Background>
            <ImageBrush ImageSource="Assets/HubPageImages/Bg.jpg"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid x:Name="resultsPanel" Grid.Row="1">
            <!--
            The body of the page in most view states uses an items controls to create multiple radio buttons
            for filtering above a horizontal scrolling grid of search results
        -->
            <Grid x:Name="typicalPanel">
                

                <ItemsControl
                    x:Name="filtersItemsControl"
                    Canvas.ZIndex="1"
                    ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                    Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                    Margin="120,0,120,30">

                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <RadioButton
                                GroupName="Filters"
                                IsChecked="{Binding Active, Mode=TwoWay}"
                                Checked="Filter_Checked"
                                Style="{StaticResource TextRadioButtonStyle}">
                                <TextBlock Text="{Binding Description}"  Margin="3,-7,3,10" Style="{StaticResource GroupHeaderTextStyle}" />
                            </RadioButton>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <GridView
                    x:Name="resultsGridView"
                    AutomationProperties.AutomationId="ResultsGridView"
                    AutomationProperties.Name="Search Results"
                    TabIndex="1"
                    Grid.Row="1"
                    Margin="0,10,0,0"
                    Padding="110,0"
                    SelectionMode="None"
                    IsSwipeEnabled="false"
                    IsItemClickEnabled="True"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplate="{StaticResource NecklacesSmallItemTemplate}"
                    ItemClick="resultsGridView_ItemClick_2">

                    <GridView.ItemContainerStyle>
                        <Style TargetType="Control">
                            <Setter Property="Height" Value="175"/>
                            <Setter Property="Margin" Value="0,0,38,8"/>
                        </Style>
                    </GridView.ItemContainerStyle>
                </GridView>
            </Grid>

            <!--
                The body of the page when snapped uses a combo box to select a filter above a
                vertical scrolling list of search results
            -->
            <Grid x:Name="snappedPanel" Visibility="Collapsed">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <ComboBox
                    ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                    Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                    Margin="20,0,20,20"
                    SelectionChanged="Filter_SelectionChanged"
                    HorizontalAlignment="Left"/>

                <ListView
                    x:Name="resultsListView"
                    AutomationProperties.AutomationId="ResultsListView"
                    AutomationProperties.Name="Search Results"
                    TabIndex="1"
                    Grid.Row="1"
                    Margin="10,0,0,0"
                    Padding="0,0,0,60"
                    SelectionMode="None"
                    IsSwipeEnabled="false"
                    IsItemClickEnabled="True"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplate="{StaticResource Standard80ItemTemplate}">

                    <ListView.ItemContainerStyle>
                        <Style TargetType="Control">
                            <Setter Property="Height" Value="70"/>
                            <Setter Property="Margin" Value="0,0,18,8"/>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>
            </Grid>
        </Grid>

        <!-- Back button, page title and subtitles-->

        <TextBlock
            x:Name="noResultsTextBlock"
            Grid.Row="1"
            Margin="120,50,0,0"
            Visibility="Collapsed"
            Style="{StaticResource SubheaderTextStyle}"
            Text="No results match your search." />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}" Margin="36,0,36,36"/>
            <Grid x:Name="LogoMain" HorizontalAlignment="Left" Height="100" VerticalAlignment="Center" Width="234" Grid.Column="1" Margin="-11,0,0,0">
                <Image x:Name="image" Stretch="Fill" Source="Assets/MainLogo.png" Margin="0"/>
            </Grid>
            <StackPanel x:Name="stackPanel" Margin="0,0,0,40" Orientation="Horizontal" VerticalAlignment="Bottom" Grid.Column="1">
            	<TextBlock x:Name="pageTitle" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"  Margin="0,0,20,0" FontSize="56"  Foreground="#DEFFFFFF" Text="Search"  />
            	<TextBlock x:Name="resultText" Text="Results for " IsHitTestVisible="false" Style="{StaticResource PageSubheaderTextStyle}" Margin="0" Foreground="#DEFFFFFF"/>
            	<TextBlock x:Name="queryText" Text="{Binding QueryText}" IsHitTestVisible="false" Style="{StaticResource PageSubheaderTextStyle}" Margin="0" Foreground="#DEFFFFFF"/>
            </StackPanel>
            <Image x:Name="image_Copy" HorizontalAlignment="Right" Height="77" VerticalAlignment="Center" Width="203" Source="Assets/Logo.png" Margin="0,31.5,40,31.5" Grid.Column="3"/>
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="filtersItemsControl" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,-3,90,30"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsGridView" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="90,240,86,56"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,50,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped in addition to more extensive changes:
                    * The query text becomes the page header
                    * A different representation for filter selection and search results is used
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="typicalPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BaselineTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="image">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Thickness>0</Thickness>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="LogoMain">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Thickness>-37,0,0,0</Thickness>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    	<DoubleAnimation Duration="0" To="40" Storyboard.TargetProperty="(TextBlock.FontSize)" Storyboard.TargetName="pageTitle" d:IsOptimized="True"/>
                    	<DoubleAnimation Duration="0" To="20" Storyboard.TargetProperty="(TextBlock.FontSize)" Storyboard.TargetName="resultText" d:IsOptimized="True"/>
                    	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="stackPanel">
                    		<DiscreteObjectKeyFrame KeyTime="0">
                    			<DiscreteObjectKeyFrame.Value>
                    				<Thickness>-23,0,0,40</Thickness>
                    			</DiscreteObjectKeyFrame.Value>
                    		</DiscreteObjectKeyFrame>
                    	</ObjectAnimationUsingKeyFrames>
                    	<DoubleAnimation Duration="0" To="40" Storyboard.TargetProperty="(Control.FontSize)" Storyboard.TargetName="backButton" d:IsOptimized="True"/>
                    	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="backButton">
                    		<DiscreteObjectKeyFrame KeyTime="0">
                    			<DiscreteObjectKeyFrame.Value>
                    				<Thickness>15,0,20,16</Thickness>
                    			</DiscreteObjectKeyFrame.Value>
                    		</DiscreteObjectKeyFrame>
                    	</ObjectAnimationUsingKeyFrames>
                    	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="pageTitle">
                    		<DiscreteObjectKeyFrame KeyTime="0">
                    			<DiscreteObjectKeyFrame.Value>
                    				<Thickness>0,0,5,0</Thickness>
                    			</DiscreteObjectKeyFrame.Value>
                    		</DiscreteObjectKeyFrame>
                    	</ObjectAnimationUsingKeyFrames>
                    	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image_Copy">
                    		<DiscreteObjectKeyFrame KeyTime="0">
                    			<DiscreteObjectKeyFrame.Value>
                    				<Visibility>Collapsed</Visibility>
                    			</DiscreteObjectKeyFrame.Value>
                    		</DiscreteObjectKeyFrame>
                    	</ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>

            <VisualStateGroup x:Name = "ResultStates">
                <VisualState x:Name="ResultsFound" />
                <!-- When there are no results, the results panel is replaced with an informational TextBlock -->
                <VisualState x:Name="NoResultsFound">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
